package com.zyy.smartcity1129.news;

import android.util.Log;
import android.view.View;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.zyy.smartcity1129.BaseFragment;
import com.zyy.smartcity1129.R;
import com.zyy.smartcity1129.data.NewsComments;
import com.zyy.smartcity1129.data.NewsContentsInfo;
import com.zyy.smartcity1129.data.newslist;
import com.zyy.smartcity1129.util.Http;
import com.zyy.smartcity1129.util.RvAd;
import com.zyy.smartcity1129.util.XCUtli;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName: News1Fragment$
 * @Description: 作用
 * @Author: letquit
 * @CreateDate: 1/12/2021$
 * @Version: 1.0
 */
public class News1Fragment extends BaseFragment {
    private int newId = NewsFragment.newsId;
    private String newname = NewsFragment.names;
    private String newType = NewsFragment.typeId;
    List<NewsComments.RowsBean> plList = new ArrayList<>();
    NewsContentsInfo.DataBean dataBean;
    List<newslist.RowsBean> newslist = new ArrayList<>();
    RvAd<NewsComments.RowsBean> plAd;
    private WebView webViewNews1;
    private RecyclerView rvNews1;
    private EditText editNews1;
    private Button btnNews1;
    private TextView tvNumNews1;
    private RecyclerView rvPLNews1;

    @Override
    protected int layId() {
        return R.layout.f_news1;
    }

    @Override
    protected String title() {
        return newname;
    }

    @Override
    protected boolean back() {
        return true;
    }

    @Override
    protected Boolean bottom() {
        return null;
    }

    @Override
    protected void init(View iv) {
        initView(iv);
        XCUtli xcUtli = new XCUtli(3);
        xcUtli.then(() -> {
            dataBean = Http.getInstance().get("/prod-api/press/press/" + newId, null, NewsContentsInfo.class, 1).getData();
            xcUtli.addint();
        });
        xcUtli.then(() -> {
            newslist = Http.getInstance().get("/prod-api/press/press/list?type=" + newType + "&top=Y", null, newslist.class, 1).getRows();
            xcUtli.addint();
        });
        xcUtli.then(() -> {
            plList = Http.getInstance().get("/prod-api/press/comments/list", null, NewsComments.class, 1).getRows();
            xcUtli.addint();
        });
        xcUtli.XCliveData.observe(getViewLifecycleOwner(), atomicInteger -> {
            if (atomicInteger.get() == 3) {
                RvAd<newslist.RowsBean> rvNewsAd = new RvAd<newslist.RowsBean>(newslist, R.layout.rv_base) {
                    private void initView(View vi) {
                        ig = (ImageView) vi.findViewById(R.id.ig);
                        tv1 = (TextView) vi.findViewById(R.id.tv1);
                        tv2 = (TextView) vi.findViewById(R.id.tv2);
                        tv3 = (TextView) vi.findViewById(R.id.tv3);
                        tv4 = (TextView) vi.findViewById(R.id.tv4);
                        tv5 = (TextView) vi.findViewById(R.id.tv5);
                        tv5.setVisibility(View.GONE);
                    }

                    private TextView tv5;
                    private TextView tv4;
                    private TextView tv3;
                    private TextView tv2;
                    private TextView tv1;
                    private ImageView ig;

                    @Override
                    protected void initR(View vi, newslist.RowsBean rowsBean, int p) {
                        initView(vi);
                        Glide.with(ig.getContext()).load(Http.base + rowsBean.getCover()).into(ig);
                        tv1.setText(rowsBean.getTitle());
                        tv2.setText(rowsBean.getAppType());
                        tv3.setText(rowsBean.getPublishDate());
                        tv4.setText("观看人数" + rowsBean.getReadNum() + "    点赞人数：" + rowsBean.getLikeNum());
                        vi.setOnClickListener(view -> {
                            NewsFragment.newsId = rowsBean.getId();
                            NewsFragment.names = rowsBean.getTitle();
                            NewsFragment.typeId = rowsBean.getType();
                            add(new News1Fragment());
                        });
                    }
                };
                rvNews1.setLayoutManager(new LinearLayoutManager(getContext()));
                rvNews1.setAdapter(rvNewsAd);
                List<NewsComments.RowsBean> plist = new ArrayList<>();
                plist.addAll(plList.stream().filter(a -> a.getNewsId() == newId).collect(Collectors.toList()));
                initAdapter(plist);
                rvPLNews1.setLayoutManager(new LinearLayoutManager(getContext()));
                rvPLNews1.setAdapter(plAd);
                tvNumNews1.setText("评论条数：" + plist.size());
                webViewNews1.loadDataWithBaseURL(Http.base, dataBean.getContent(), "text/html", "utf-8", null);
            }
        });
    }

    private void initAdapter(List<NewsComments.RowsBean> plist) {
        plAd = new RvAd<NewsComments.RowsBean>(plist, R.layout.rv_base) {
            private TextView tv5;
            private TextView tv4;
            private TextView tv3;
            private TextView tv2;
            private TextView tv1;
            private ImageView ig;

            private void initView(View vi) {
                ig = (ImageView) vi.findViewById(R.id.ig);
                tv1 = (TextView) vi.findViewById(R.id.tv1);
                tv2 = (TextView) vi.findViewById(R.id.tv2);
                tv3 = (TextView) vi.findViewById(R.id.tv3);
                tv4 = (TextView) vi.findViewById(R.id.tv4);
                tv5 = (TextView) vi.findViewById(R.id.tv5);
                ig.setVisibility(View.GONE);
                tv3.setVisibility(View.GONE);
                tv4.setVisibility(View.GONE);
                tv5.setVisibility(View.GONE);
            }


            @Override
            protected void initR(View vi, NewsComments.RowsBean rowsBean, int p) {
                initView(vi);
                Log.i("TAG", rowsBean.getUserName());
                tv1.setText(rowsBean.getUserName());
                tv2.setText("\t\t\t\t" + rowsBean.getContent());
            }
        };
    }

    private void initView(View iv) {
        webViewNews1 = (WebView) iv.findViewById(R.id.webViewNews1);
        rvNews1 = (RecyclerView) iv.findViewById(R.id.rv_News1);
        editNews1 = (EditText) iv.findViewById(R.id.editNews1);
        btnNews1 = (Button) iv.findViewById(R.id.btnNews1);
        tvNumNews1 = (TextView) iv.findViewById(R.id.tv_numNews1);
        rvPLNews1 = (RecyclerView) iv.findViewById(R.id.rv_PLNews1);
    }
}
